package sk.vrto.web.views;

import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PopupView;
import sk.vrto.domain.Contact;
import sk.vrto.web.presenters.ContactPresenter;

public class ContactView extends HorizontalLayout {

    private final Contact contact;
    private final ContactPresenter presenter;
    private final ContactViewPopup viewPopup;

    public ContactView(final Contact contact, ContactPresenter presenter) {
        this.contact = contact;
        this.presenter = presenter;
        presenter.setView(this);

        addStyleName("contact");
        setWidth("120px");

        viewPopup = new ContactViewPopup(contact, presenter);
        PopupView popupView = new PopupView(intelligentName(contact.getName()), viewPopup);
        popupView.setStyleName("contact-font");
        popupView.setHideOnMouseOut(false);
        popupView.setWidth("100%");
        addComponent(popupView);
    }

    public ContactViewPopup getViewPopup() {
        return viewPopup;
    }

    private String intelligentName(String name) {
        if (15 < name.length()) {
            return name.substring(0, 15) + "...";
        }
        return name;
    }

    public Contact getContact() {
        return contact;
    }
}
